<template>
    <div style="margin-top: 14px;height: 200px">
        <div style="display: flex;align-items: center">
            <span>支持上传.xls,.xlsx格式的文件</span>
            <div style="padding: 10px 20px">导入模版下载：<span><a-button style="padding: 0"
                                                                   type="link"
                                                                   @click="downloadTemplate">模版.xls</a-button></span>
            </div>
        </div>
        <a-upload
            :before-upload="beforeUpload"
            :file-list="fileList"
            :remove="handleRemove"
            accept=".xls,.xlsx"
        >
            <a-button type="primary">
                <a-icon type="upload"/>
                添加导入文件
            </a-button>
        </a-upload>
    
    </div>
</template>

<script>
import {api_enterpriseLib2} from '@/api/api'
import {DownloadFile} from "@/utils/common";

export default {
    name: 'Index',
    props: {
        url: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            fileList: [],
            loading: false
        }
    },
    methods: {
        /**
         *  上传excel
         */
        async httpRequest() {
            console.log(this.fileList)
            if (!this.fileList.length) {
                this.$message.error('请选择上传的文件')
                return Promise.reject('请选择上传的文件')
            }
            const file = this.fileList[0]
            const list = ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel']
            if (list.indexOf(file.type) === -1) {
                this.$message.error('上传格式不正确')
                return Promise.reject('上传格式不正确')
            }
            this.loading = true
            const formData = new FormData()
            formData.append('file', file)
            await api_enterpriseLib2.uploadExcel(formData)
            this.loading = false
            this.$message.success('导入成功')
            this.fileList = []
            return true
        },
        beforeUpload(file) {
            this.fileList = [file]
            console.log(this.fileList)
            return false
        },
        handleRemove() {
            this.fileList = []
        },
        async downloadTemplate() {
            const res = await api_enterpriseLib2.exportTemplate()
            DownloadFile(res, '企业库模版.xls')
        }
    }
}
</script>

<style lang='scss' scoped>

</style>
